package com.uyaer.wheel.display.scene
{
	import com.uyaer.wheel.common.Common;
	import com.uyaer.wheel.common.UserData;
	import com.uyaer.wheel.events.EventType;
	import com.uyaer.wheel.menu.MenuType;
	import com.uyaer.wheel.plus.AD;
	import com.uyaer.wheel.res.ResManager;
	import com.uyaer.wheel.tick.TimerManager;
	
	import flash.geom.Point;
	
	import starling.animation.Transitions;
	import starling.core.Starling;
	import starling.display.Image;
	import starling.display.MovieClip;
	import starling.display.Quad;
	import starling.display.Sprite;
	import starling.events.Event;
	import starling.events.Touch;
	import starling.events.TouchEvent;
	import starling.events.TouchPhase;
	import starling.text.TextField;
	import starling.textures.Texture;
	import starling.utils.HAlign;

	public class GameOver extends Sprite implements IScene
	{

		private var offset:Number = 7;
		private var ball:MovieClip;
		private var ballTextures:Vector.<Texture>;
		private var title:Image;
		private var con:Sprite;
		private var scoreTF:TextField;
		private var score:int = 0; 
		private var star:Image;
		private var backHomeBtn:Image;
		private var replayBtn:Image;
		private var perScore:int = 3;
		private var starPos:Point = new Point();

		public function GameOver()
		{
			if(stage){
				onAdded();
			}else{
				this.addEventListener(Event.ADDED_TO_STAGE,onAdded);
			}
		}
		
		private function onAdded():void{
			this.removeEventListener(Event.ADDED_TO_STAGE,onAdded);
			layout();		
			stage.addEventListener(TouchEvent.TOUCH,onTouch);
		}
		
		private function onTouch(e:TouchEvent):void{
			var touch:Touch = e.getTouch(this);
			if(touch && touch.phase == TouchPhase.BEGAN){
				switch(e.target){
					case replayBtn:
						replayBtn.color = 0x62e358;
						break;
					case backHomeBtn:
						backHomeBtn.color = 0x62e358;
						break;
				}
			}else if(touch && touch.phase == TouchPhase.ENDED){
				switch(e.target){
					case replayBtn:
						replayBtn.color = 0x29f31a;
						stage.dispatchEventWith(EventType.MENU_SELECT,false,MenuType.REPLAY_GAME);
						break;
					case backHomeBtn:
						backHomeBtn.color = 0x29f31a;
						stage.dispatchEventWith(EventType.MENU_SELECT,false,MenuType.SHOW_HOME);
						break;
				}
			}
		}
		
		private function layout():void{
			var mask:Quad = new Quad(Common.W,Common.H,0x0);
			mask.alpha = 0.55;
			addChild(mask);
			con = new Sprite();
			con.y = Common.H * 0.4;
			addChild(con);
			var bg:Image = new Image(ResManager.me.getTexture("menu_gameover_bg"));
			con.addChild(bg);
			//添加文字
			title = new Image(ResManager.me.getTexture("menu_gameover"));
			title.alpha = 0.01;
			title.x = 202;
			title.y = 25;
			con.addChild(title);
			
			ballTextures = ResManager.me.getTextures("menu_ball");
			ball = new MovieClip(ballTextures,6);
			ball.pivotY = ball.height;
			ball.x = 65;
			ball.y = 200;
			con.addChild(ball);
			
			scoreTF = new TextField(con.width*0.4,70,"0",Common.FONT,60,0xffff00);
			scoreTF.x = con.width * 0.45;
			scoreTF.y = 155;
			scoreTF.hAlign = HAlign.LEFT;
			con.addChild(scoreTF);
			
			star = new Image(ResManager.me.getTexture("menu_star"));
			star.scaleX = star.scaleY = Common.S;
			
			replayBtn = new Image(ResManager.me.getTexture("menu_replay"));
			replayBtn.x = 130;
			replayBtn.y = 267;
			replayBtn.scaleX = replayBtn.scaleY = 0.67;
			replayBtn.color = 0x29f31a;
			con.addChild(replayBtn);
			
			backHomeBtn = new Image(ResManager.me.getTexture("menu_back_home"));
			backHomeBtn.x = 352;
			backHomeBtn.y = 265;
			backHomeBtn.scaleX = backHomeBtn.scaleY = 0.67;
			backHomeBtn.color = 0x29f31a;
			con.addChild(backHomeBtn);
			
			con.scaleX = con.scaleY = Common.S;
			con.pivotY = con.height >>1;
			
			starPos.x = con.width * 0.4-star.width;
			starPos.y = Common.H*0.4 - (con.height*0.5 - scoreTF.y-3)*Common.S;
		}
		
		private function onFrameHandler():void{
			ball.y = 142 - (ball.currentFrame % ballTextures.length) * offset;
		}
		
		public function start():void{
			scoreTF.text = "";
			ball.currentFrame = 0;
			ball.addEventListener(Event.ENTER_FRAME,onFrameHandler);
			
			con.y = Common.H+con.width;
			Starling.juggler.tween(con,0.8,{"y":Common.H*0.4,
							"transition":Transitions.EASE_OUT,
							"onComplete":function():void{Starling.juggler.add(ball);}});
			//显示游戏结束
			title.y = -200;
			con.addChild(title);
			title.alpha = 0.01;
			Starling.juggler.tween(title,0.8,{"y":31,"delay":1,"alpha":1,
							"transition":Transitions.EASE_OUT});
			//添加星星
			
			addChild(star);
			star.x = 8;
			star.y = 8;
			
			var dx:int = starPos.x;
			var dy:int = starPos.y;
			
			perScore = int(UserData.me.starCount/100)+1;
			
			Starling.juggler.tween(star,1,{"delay":2,"x":dx,"y":dy,
						"transition":Transitions.EASE_OUT,
						"onComplete":function():void{
							score = 0;
							TimerManager.addLoop(changeScore,15);
						}});
			
			//保存数据 
			if(UserData.me.maxStarCount<UserData.me.starCount){
				UserData.me.maxStarCount = UserData.me.starCount
				UserData.me.save();
				this.dispatchEventWith(EventType.MAX_SCORE_CHANGE);
			}
		}
		
		private function changeScore():void{
			score += perScore;
			if(score>=UserData.me.starCount){
				score = UserData.me.starCount;
				TimerManager.removeLoop(changeScore);
				//todo 链接网络，提交分数
			}
			scoreTF.text = score+"";
		}
		
		public function stop():void{
			Starling.juggler.remove(ball);
			ball.removeEventListener(Event.ENTER_FRAME,onFrameHandler);
		}
	}
}